home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / fido10as.zip / FIDOADDR.CPP next >
C/C++ Source or Header  |  1997-04-08  |  4KB  |  183 lines

  1. /*
  2.  * FidoNet(tm) Address Parsing Class (full support for 5D format)
  3.  *
  4.  * Copyright (c) 1995, 1997 by Branislav L. Slantchev
  5.  * A fine product of Silicon Creations, Inc. (gargoyle)
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the License which accompanies this
  9.  * software. This library is distributed in the hope that it will
  10.  * be useful, but without any warranty; without even the implied
  11.  * warranty of merchantability or fitness for a particular purpose.
  12.  *
  13.  * You should have received a copy of the License along with this
  14.  * library, in the file LICENSE.DOC; if not, write to the address
  15.  * below to receive a copy via electronic mail.
  16.  *
  17.  * You can reach Branislav L. Slantchev (Silicon Creations, Inc.)
  18.  * at bslantch@cs.angelo.edu. The file SUPPORT.DOC has the current
  19.  * telephone numbers and the postal address for contacts.
  20. */
  21.  
  22. #include "fidoaddr.h"
  23.  
  24. #ifndef EOS
  25.     #define EOS '\0'
  26. #endif
  27.  
  28. #ifndef PB_SDK
  29.     #include <stdio.h>
  30.     #include <string.h>
  31.     #include <stdlib.h>
  32. #else
  33.     #include "pblibc.h"
  34. #endif
  35.  
  36. fido_address::fido_address()
  37.     :m_zone(0)
  38.     ,m_net(0)
  39.     ,m_node(0)
  40.     ,m_point(0)
  41. {
  42.     m_domain[0] = EOS;
  43. }
  44.  
  45. fido_address::fido_address(int zone, int net, int node, int point)
  46.     :m_zone(zone)
  47.     ,m_net(net)
  48.     ,m_node(node)
  49.     ,m_point(point)
  50. {
  51.     m_domain[0] = EOS;
  52. }
  53.  
  54. fido_address::fido_address(int z, int net, int node, int p, const char *d)
  55.     :m_zone(z)
  56.     ,m_net(net)
  57.     ,m_node(node)
  58.     ,m_point(p)
  59. {
  60.     strcpy(m_domain, d);
  61. }
  62.  
  63. fido_address::fido_address(const fido_address &address)
  64.     :m_zone(address.m_zone)
  65.     ,m_net(address.m_net)
  66.     ,m_node(address.m_node)
  67.     ,m_point(address.m_point)
  68. {
  69.     strcpy(m_domain, address.m_domain);
  70. }
  71.  
  72. fido_address::fido_address(const char *ascii_address)
  73. {
  74.     split(ascii_address);
  75. }
  76.  
  77. fido_address&
  78. fido_address::operator=(const fido_address &address)
  79. {
  80.     m_zone  = address.m_zone;
  81.     m_net   = address.m_net;
  82.     m_node  = address.m_node;
  83.     m_point = address.m_point;
  84.     strcpy(m_domain, address.m_domain);
  85.     return *this;
  86. }
  87.  
  88. char*
  89. fido_address::merge(char *buf) const
  90. {
  91.     buf[0] = EOS;
  92.     if( m_zone ) sprintf(buf, "%u:", m_zone);
  93.     if( m_net ) sprintf(buf + strlen(buf), "%u/", m_net);
  94.     sprintf(buf + strlen(buf), "%u", m_node);
  95.     if( m_point ) sprintf(buf + strlen(buf), ".%u", m_point);
  96.     if( EOS != m_domain[0] ) sprintf(buf + strlen(buf), "@%s", m_domain);
  97.  
  98.     return buf;
  99. }
  100.  
  101. void
  102. fido_address::split(const char *ascii_address)
  103. {
  104.     const char *p, *p2;
  105.  
  106.     m_zone = m_net = m_node = m_point = 0;
  107.     m_domain[0] = EOS;
  108.  
  109.     // check for zone first and set it, of found; note that
  110.     // atol() stops on invalid chars, which is the ':' in our case
  111.     if( 0 != (p = strchr(ascii_address, ':')) )
  112.     {
  113.         m_zone = (int)atol(ascii_address);
  114.         p++;
  115.     }
  116.     else p = ascii_address;
  117.  
  118.     // here 'p' points either past the zone in the address
  119.     // or to the beginning of the string address, check for net
  120.     if( 0 != (p2 = strchr(p, '/')) )
  121.     {
  122.         m_net = (int)atol(p);
  123.         p = p2 + 1;
  124.     }
  125.  
  126.     // here we have 'p' pointing to the node (which must
  127.     // be present in any of the valid address combinations)
  128.     m_node = (int)atol(p);
  129.  
  130.     // now try to get the point if there is any
  131.     if( 0 != (p2 = strchr(p, '.')) ) m_point = (int)atol(p2+1);
  132.  
  133.     // now see if we have a domain designation
  134.     if( 0 != (p2 = strchr(p, '@')) ) strcpy(m_domain, p2 + 1);
  135. }
  136.  
  137. char*
  138. fido_address::merge2d(char *buf) const
  139. {
  140.     sprintf(buf, "%u/%u", m_net, m_node);
  141.     return buf;
  142. }
  143.  
  144. char*
  145. fido_address::merge3d(char *buf) const
  146. {
  147.     sprintf(buf, "%u:%u/%u", m_zone, m_net, m_node);
  148.     return buf;
  149. }
  150.  
  151. char*
  152. fido_address::merge4d(char *buf) const
  153. {
  154.     sprintf(buf, "%u:%u/%u.%u", m_zone, m_net, m_node, m_point);
  155.     return buf;
  156. }
  157.  
  158. char*
  159. fido_address::merge5d(char *buf) const
  160. {
  161.     if( EOS == m_domain[0] )
  162.     {
  163.         merge4d(buf);
  164.     }
  165.     else
  166.     {
  167.         sprintf(buf, "%u:%u/%u.%u@%s", m_zone,m_net,m_node,m_point,m_domain);
  168.     }
  169.     return buf;
  170. }
  171.  
  172. char*
  173. fido_address::domain(char *buf) const
  174. {
  175.     return strcpy(buf, m_domain);
  176. }
  177.  
  178. void
  179. fido_address::set_domain(const char *domain)
  180. {
  181.     strcpy(m_domain, domain);
  182. }
  183.